package cn.itcast.bos.service.base.impl;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.AreaRepository;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.AreaService;
@Service 
@Transactional
public class AreaServiceImpl implements AreaService {

	@Autowired
	private AreaRepository areaRepository;
	@Override
	public void save(List<Area> areas) {
		areaRepository.save(areas);
		
	}
	@Override
	public Page<Area> findAll(Specification<Area> specification, Pageable page) {
		return areaRepository.findAll(specification,page);
	}
	@Override
	public void save(Area model) {
		model.setId("123");
		areaRepository.save(model);
	}
	@Override
	public List<Area> findAll() {

		Specification<Area> specification=new Specification<Area>() {

			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate p = cb.isEmpty(root.get("subareas").as(Set.class));
				return p;
			}
		};
		
		return areaRepository.findAll(specification);
	}
	
}
